package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.model.ProductModel;
import com.bytz.modules.cms.product.service.IMallProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(tags = "产品相关")
@RequestMapping("/product")
public class MallProductController {

    @Autowired
    private IMallProductService productService;

    @ApiOperation(value = "搜索产品基础信息", notes = "左模糊查询")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Result<List<ProductModel>> searchProduct(@RequestParam String code) {

        return Result.ok(productService.searchProduct(code));
    }

    @ApiOperation("根据code获取商品详情及对应经销商的价格")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Result<List<ProductModel>> getProductsByCode(@RequestParam List<String> codes) {

        return Result.ok(productService.getProductDetailByNumbers(codes));
    }

    @ApiOperation("查询经销商产品列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ProductModel>> page(Product product,
                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            HttpServletRequest req) throws Exception {
        return Result.ok(productService.queryPage(new Page<>(pageNo, pageSize), product, req.getParameterMap()));
    }
}
